package co.baiku.boot.config;

import co.baiku.boot.common.tools.StringTools;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.internal.util.AntCertificationUtil;
import com.alipay.api.internal.util.file.FileUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.security.Security;
import java.text.MessageFormat;

/**
 * 支付宝支付
 */
@ConfigurationProperties(prefix = "ajavaer.alipay")
public class AlipayConfig implements Serializable {
    // 小程序 APPID
    private String appid;
    // 应用公匙
    private String publicKey;
    // 应用私匙
    private String privateKey;
    // 支付宝公匙
    private String alipayPublicKey;
    // 是否采用证书的方式
    private boolean useCert;
    // 支付宝公钥证书地址
    private String alipayCertPublicKeyPath;
    // 支付宝根证书地址
    private String alipayRootCertPath;
    // 支付宝根证书SN
    private String alipayRootCertSn;
    // 支付宝应用公钥证书地址
    private String appCertPublicKeyPath;
    // 支付宝应用公钥证书SN
    private String appCertSn;
    // 回调地址
    private String notifyUrl;
    private String charset = StringTools.UTF8.name();
    private String signType = "RSA2";
    private String aesKey;
    private String gatewayUrl = "https://openapi.alipay.com/gateway.do";
    private String sellerId;//支付宝授权转支付 卖家支付宝账户id
    private String serviceId;//芝麻信用信用服务ID
    //生活号跳转链接
    private String lifeFollowUri = "alipays://platformapi/startapp?appId=20000042&publicBizType=LIFE_APP&publicId={0}";

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public String getGatewayUrl() {
        return gatewayUrl;
    }

    public void setGatewayUrl(String gatewayUrl) {
        this.gatewayUrl = gatewayUrl;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public String getSignType() {
        return signType;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }

    public String getAesKey() {
        return aesKey;
    }

    public void setAesKey(String aesKey) {
        this.aesKey = aesKey;
    }

    public String getAlipayPublicKey() {
        return alipayPublicKey;
    }

    public void setAlipayPublicKey(String alipayPublicKey) {
        this.alipayPublicKey = alipayPublicKey;
    }

    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }

    public String getPublicKey() {
        return publicKey;
    }

    public boolean needEncrypt() {
        return StringTools.isNotBlank(this.aesKey);
    }

    public String getSellerId() {
        return sellerId;
    }

    public void setSellerId(String sellerId) {
        this.sellerId = sellerId;
    }

    public String getServiceId() {
        return serviceId;
    }

    public void setServiceId(String serviceId) {
        this.serviceId = serviceId;
    }

    public String getLifeFollowUri() {
        return MessageFormat.format(lifeFollowUri, appid);
    }

    public void setLifeFollowUri(String lifeFollowUri) {
        this.lifeFollowUri = lifeFollowUri;
    }


    public boolean isUseCert() {
        return useCert;
    }

    public void setUseCert(boolean useCert) {
        this.useCert = useCert;
    }

    public String getAlipayCertPublicKeyPath() {
        return alipayCertPublicKeyPath;
    }

    public void setAlipayCertPublicKeyPath(String alipayCertPublicKeyPath) {
        this.alipayCertPublicKeyPath = alipayCertPublicKeyPath;
    }

    public String getAlipayRootCertPath() {
        return alipayRootCertPath;
    }

    public void setAlipayRootCertPath(String alipayRootCertPath) {
        this.alipayRootCertPath = alipayRootCertPath;
    }

    public String getAppCertPublicKeyPath() {
        return appCertPublicKeyPath;
    }

    public void setAppCertPublicKeyPath(String appCertPublicKeyPath) {
        this.appCertPublicKeyPath = appCertPublicKeyPath;
    }

    public String getAlipayRootCertSn() {
        return alipayRootCertSn;
    }

    public void setAlipayRootCertSn(String alipayRootCertSn) {
        this.alipayRootCertSn = alipayRootCertSn;
    }

    public String getAppCertSn() {
        return appCertSn;
    }

    public void setAppCertSn(String appCertSn) {
        this.appCertSn = appCertSn;
    }

    public boolean check(){
        if(useCert){
            return !StringTools.isBlank(this.appid,this.privateKey,this.alipayCertPublicKeyPath,this.alipayRootCertPath, this.appCertPublicKeyPath,this.sellerId);
        }else{
            return !StringTools.isBlank(this.appid,this.privateKey,this.publicKey,this.alipayPublicKey,this.sellerId);
        }
    }

}
